<?php

namespace App\Service\Auth;

use App\Constants\ErrorCode;
use App\Helpers\Helper;
use App\Model\User;
use App\Service\BaseService;

/**
 * 绑定手机号
 */
class BindPhoneService extends BaseService
{
    protected function RoVerifyList()
    {
        return [
            'phone' => "required|string",
            'captcha' => "required|string",
        ];
    }

    public function handle()
    {
        $userId = $this->getUserId();
        $phone = $this->request->input('phone');
        $captcha = $this->request->input('captcha');
        //检测验证码
        $check = Helper::checkCaptcha($phone, $captcha);
        if (!check_dev_env() && !$check && !$this->checkWhitePhone($phone)) {
            return $this->setError(ErrorCode::CAPTCHA_ERR, '验证码错误');
        }
        //该手机号已经绑定过其他账号
        $check = User::checkPhone($phone);
        if ($check) return $this->setError(ErrorCode::PHONE_BINDED, '该手机号已经绑定过其他账号了~');

        //绑定手机号
        $ret['result'] = User::updatePhone($userId, $phone) ? 1 : 0;
        log_user_result($userId, $ret);
        return $ret;
    }
}
